package cn.funeralobjects.util.converter;

import cn.funeralobjects.util.CollectionUtils;

import java.util.Set;
import java.util.stream.Collectors;

/**
 * 默认的Set字串对数据库字串的转换器
 *
 * @author FuneralObjects
 * Create date: 2020/4/14 5:58 PM
 */
public class DefaultSetStringConverter extends BaseSetConverter<String, String> {
    protected static final String SPLIT_DELIMITER = "\\u007C";
    protected static final String DELIMITER = "|";
    protected static final String NULL_VALUE = "!--";

    @Override
    public String convertToDatabaseColumn(Set<String> attribute) {
        return attribute == null ? NULL_VALUE : CollectionUtils.join(attribute, DELIMITER);
    }

    @Override
    public Set<String> convertToEntityAttribute(String dbData) {
        return CollectionUtils.split(dbData, SPLIT_DELIMITER).parallelStream().collect(Collectors.toSet());
    }
}
